#coding=utf-8
import stockdb.misc as misc
import strategy.basic as basic

#该策略适合大跌后企稳时使用，似乎.

def diwei_filter1(td, n, thr):
    close = td.price.close
    
    tds = td.pre_n(n-1)
    
    cs = [td.close for td in tds]
    cs.append(close)
    
    cs.sort()
    
    i = cs.index(close)    
    
    if i > thr: return False
    return True
    
def diwei_filter2(td, n=20, a=1.05, b=1.15):
    
    lp = td.lowest(n)
    if not lp:return False
    c = td.price.close
    
    if c > a * lp and c < b * lp:
        #print lp
        return True
    
    return False
    
def volume_filter1(td, n1, n2, m):
    v1 = td.avg_traderate(n1)
    v2 = td.avg_traderate(n2)
    
    if v1 > m * v2:return True
    return False


if __name__ == '__main__':
    date = '2008-08-15'
    date = misc.today
    print date
    
    tds = misc.find_tds_on(date)
    #print len(tds)
    
    tf = basic.TDFilter(tds)

    tds = tf.filter_by(basic.st_filter).\
             filter_by(basic.tdattr_filter, 'close', 2, 35).\
             filter_by(basic.tdattr_filter, 'trade_rate', 2, 8).\
             filter_by(volume_filter1, 3, 5, 1.2).\
             filter_by(diwei_filter2).\
             all()
             
    #print len(tds)         
           
    for t in tds:
        s = t.fins
        p = t.price
        
        print s.code, s.name,'%.2f%%'%p.trade_rate, s.ts/10000
        p2 = t.next
        if p2:
            print p2.price.change
